﻿
Public Class eventDriver
    Private Shared ReadOnly [global] As eventDriver = Nothing

    Shared Sub New()
        [global] = New eventDriver()
    End Sub

    Public Shared Function register(ByVal resolver As resolver) As Boolean
        If resolver Is Nothing Then
            Return False
        Else
            resolver.register([global])
            Return True
        End If
    End Function

    Public Shared Function register() As Boolean
        Return register(resolver.global)
    End Function

    Private Sub work(ByVal ec As eventComb)
        ec.do()
    End Sub

    Private Sub work(ByVal obj As Object)
        work(convertor.cast(Of eventComb)(obj))
    End Sub

    Private Shared Function eventComb_valid(ByVal ec As eventComb) As Boolean
        Return Not ec Is Nothing AndAlso ec.not_pending()
    End Function

    Public Function begin(ByVal ec As eventComb) As Boolean
        If eventComb_valid(ec) Then
            resolve(Of ithreadpool)().insertJob(AddressOf work, ec)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function begin(ByVal ec As eventComb, ByVal timeoutMs As Int64) As Boolean
        If eventComb_valid(ec) Then
            ec.setTimeout(timeoutMs)
            assert(begin(ec))
            Return True
        Else
            Return False
        End If
    End Function
End Class
